package study.spring.transaction;

import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class JdbcServiceTransactionManager implements JdbcService {
	// private JdbcTemplate template;

	private InsertOne insertOne;
	private InsertTwo insertTwo;

	private PlatformTransactionManager transactionManager;

	public void insert(final EditLink t) {

		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

		TransactionStatus status = transactionManager.getTransaction(def);

		String result = "";
		try {

			insertOne.add();
			insertTwo.add();

			if (null == t.getName()) {
				throw new RuntimeException();
			}
			result = "sucess";
			transactionManager.commit(status);
		} catch (Exception e) {
			transactionManager.rollback(status);
			result = "rollback";
		}

		System.out.println(result);
	}

	// ============================

	public PlatformTransactionManager getTransactionManager() {
		return transactionManager;
	}

	public void setTransactionManager(PlatformTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}

	public InsertOne getInsertOne() {
		return insertOne;
	}

	public void setInsertOne(InsertOne insertOne) {
		this.insertOne = insertOne;
	}

	public InsertTwo getInsertTwo() {
		return insertTwo;
	}

	public void setInsertTwo(InsertTwo insertTwo) {
		this.insertTwo = insertTwo;
	}

}
